﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using OnlineTestBLL;
using OnlineTestModel;

namespace OnlineTestUI.BackManager
{
    public partial class updatecompletion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["QuestionId"]))
                {
                    int questionId = Convert.ToInt32(Request.QueryString["QuestionId"]);
                    Questions q = new Questions();
                    QuestionsBLL qb = new QuestionsBLL();
                    q = qb.GetModel(questionId);
                    hfQuestionId.Text = q.Id.ToString();
                    tbquestion.Text = q.QuestionName;
                    hfVersionNumber.Text = (q.VersionNumber+1).ToString();//更新则版本号加一
                    tbpredifficult.Text = q.PreDifficult;
                    tbchapter.Text = q.Chapter;
                    tbpart.Text = q.Part;
                    tbblankline.Text = q.BlankLine.ToString();
                    taanswer.Text = q.CorrectAnswer;
                    tbpartitiondegree.Text = q.PartitionDegree;
               }
               
            }
        }
        protected void BtAdd_Click(object sender, EventArgs e)
        {
            
            QuestionsBLL qb = new QuestionsBLL();
            Questions q = new Questions();
            q.QuestionName = tbquestion.Text;
            q.VersionNumber = Convert.ToInt32(hfVersionNumber.Text);
            q.PreDifficult = tbpredifficult.Text;
            q.Chapter = tbchapter.Text;
            q.Part = tbpart.Text;
            q.QuestionTypeId = 4;
            q.BlankLine = Convert.ToInt32(tbblankline.Text);
            q.CorrectAnswer = taanswer.Text;
            q.PartitionDegree = tbpartitiondegree.Text;
            q.ParentId = Convert.ToInt32(hfQuestionId.Text);//将之前的题目id设置为父id
            q.Status = 1;
            //每次对试题的修改都是创建新题
            using (TransactionScope ts = new TransactionScope())
            {
                bool oldresult=qb.UpdateStatus(0,q.ParentId);
                int result = qb.Add(q);
                if (result > 0&&oldresult)
                {
                    ts.Complete();
                    Alert.Show("修改填空题成功");
                    PageContext.Redirect("question.list?id=2");
                }
                else
                {
                    Alert.Show("出现错误");
                }
            }
        }
    }
}